/*
 * @Create Date：2011-10-11
 * @author :jonny
 * @version 1.0
 * @CopyRight: 
 */

package com.first.core.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import sun.misc.BASE64Encoder;

/**
 * 
 * <p>
 * 公共工具类
 * </p>
 *
 * @author 	jonny
 * @version 1.0
 */
public class CommonUtils {

	/**
	 * convert unicode to GB2312 <br>
	 * <b>modify description：</b>因项目中已经添加过滤器，而代码中多处用到此函数进行转码，故将此方法修改为返回原字符串
	 * 
	 * @return String
	 * @param sGb
	 *            String
	 * 
	 */
	public static String toUnicode(String sGb) {
		if (sGb == null) {
			return null;
		}
		if (sGb.trim().length() == 0) {
			return sGb;
		}

		String sUnicode = "";
		try {
			sUnicode = new String(sGb.getBytes("utf-8"), "GBK");
		} catch (Exception e) {
			return "";
		}
		return sUnicode;
	}

	public static String nullToJing(String str) {
		if (str == null || str.equals("") || str.length() == 0
				|| str.equals("null")) {
			return "#";
		} else {
			return str;
		}
	}

	/**
	 * convert GB2312 to Unicode
	 * 
	 * @return String
	 * @param sGb
	 *            String
	 */
	public static String convertString(String sUnicode) {
		String sTemp = sUnicode;
		if (sTemp == null) {
			return null;
		}
		if (sTemp.trim().length() == 0) {
			return "";
		}

		String sGBStr = "";
		try {
			sGBStr = new String(sTemp.getBytes("GBK"), "ISO-8859-1");
		} catch (Exception e) {
			return "";
		}
		return sGBStr;
	}

	private static final String toHex(byte[] hash) {
		StringBuffer buf = new StringBuffer(hash.length * 2);
		int i;

		for (i = 0; i < hash.length; i++) {
			if (((int) hash[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString((int) hash[i] & 0xff, 16));
		}
		return buf.toString();
	}

	public synchronized static final String hash(String data) {
		MessageDigest digest = null;
		try {
			digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			System.out.println("没有MD5算法 " + e);
		}

		// 以下为加密
		digest.update(data.getBytes());
		return toHex(digest.digest());
	}

	/* split the string to string array */
	public static String[] split(String s, String s1) {
		int i = 0;
		int j = 0;
		boolean flag = false;
		int i1 = s1.length();
		if (s.compareTo("") != 0) {
			if (s.indexOf(s1) != -1) {
				j = s.indexOf(s1);
				int j1 = 1;
				do {
					if (s.indexOf(s1, j + i1) != -1) {
						j = s.indexOf(s1, j + i1);
						i = j1;
					} else {
						i += 2;
						break;
					}
					j1++;
				} while (true);
			} else {
				i = 1;
			}
		} else {
			i = 0;
		}
		j = 0;
		flag = false;
		String as[] = new String[i];
		if (s.compareTo("") != 0) {
			if (s.indexOf(s1) != -1) {
				int k = s.indexOf(s1);
				as[0] = s.substring(0, k);
				int k1 = 1;
				do {
					if (s.indexOf(s1, k + i1) != -1) {
						int l = s.indexOf(s1, k + i1);
						as[k1] = s.substring(k + i1, l);
						k = s.indexOf(s1, k + i1);
					} else {
						as[k1] = s.substring(k + i1, s.length());
						break;
					}
					k1++;
				} while (true);
			} else {
				as[0] = s.substring(0, s.length());
				return as;
			}
		} else {
			return as;
		}
		return as;
	}

	public static String Array2String(String[] sfile, String split) {
		String _string = "";

		if (sfile != null) {
			for (int i = 0; i < sfile.length - 1; i++) {
				_string = _string + sfile[i] + split;
			}
			_string = _string + sfile[sfile.length - 1];
		}
		return _string;
	}

	/* deal the null */
	public static String dealNull(String s) {
		String s1 = null;
		if (s == null || s.equals("null"))
			s1 = "";
		else
			s1 = s;
		return s1;
	}

	public static String dealFloat(String s) {
		String s1 = null;
		float temp = 0;
		if (s == null || s.equals("null"))
			s1 = "";
		else {
			temp = Float.parseFloat(s);
			s1 = String.valueOf(temp);
		}
		return s1;
	}

	/* deal the null to a SPACE */
	public static String null2Space(String s) {
		String s1 = null;
		if (s == null || s.equals("null") || s.equals(""))
			s1 = "&nbsp;";
		else
			s1 = s;
		return s1;
	}

	/* replace the s1 in s with s2 */
	public static String replace(String s, String s1, String s2) {
		String as[] = split(s, s1);
		String s3 = null;
		if (as.length != 0) {
			s3 = as[0];
			for (int i = 0; i < as.length - 1; i++)
				s3 = dealNull(s3) + s2 + as[i + 1];

		}
		return dealNull(s3);
	}

	/* replace the ' with \' */
	public String toOracleStr(String s) {
		if (s != null)
			s = replace(s, "'", "''");
		return s;
	}

	/* 对字符串进行编码 */
	public String htmlEncode(String s) {
		s = replace(s, "&", "&amp;");
		s = replace(s, "&amp;amp;", "&amp;");
		s = replace(s, "&amp;quot;", "&quot;");
		s = replace(s, "\"", "&quot;");
		s = replace(s, "&amp;lt;", "&lt;");
		s = replace(s, "<", "&lt;");
		s = replace(s, "&amp;gt;", "&gt;");
		s = replace(s, ">", "&gt;");
		s = replace(s, "&amp;nbsp;", "&nbsp;");
		s = replace(s, " ", "&nbsp;");
		return s;
	}

	public String print(Object o) {
		if (o == null) {
			return "&nbsp;";
		}

		String temp[] = split((String) o, "-");
		if (temp.length == 3) {
			String inTemp[] = split(temp[2], ":");
			if (inTemp.length == 3) {
				return o.toString().substring(0, 10);
			}// if
		}// if
		return o.toString();
	}

	public static String filter(String temp) {

		StringBuffer filtered = new StringBuffer(temp.length());
		char c;
		for (int i = 0; i < temp.length(); i++) {
			c = temp.charAt(i);
			if (c == '<') {
				filtered.append("&lt;");
			} else if (c == '>') {
				filtered.append("&gt;");
			} else if (c == '"') {
				filtered.append("&quot;");
			} else if (c == '&') {
				filtered.append("&amp;");
			} else {
				filtered.append(c);
			}
		}
		return filtered.toString();
	}

	public static String insteadLike(String sql) {

		int count = 0;
		int begin = 0;
		int end = 0;
		int index = 0;
		begin = sql.indexOf("%");
		end = sql.lastIndexOf("%");
		index = sql.indexOf("%", begin + 1);
		if ((index < end) && (index > begin)) {
			sql = insteadSlash(sql);
		}
		while ((index < end) && (index > begin)) {
			sql = sql.substring(0, index) + "\\" + sql.substring(index);
			index = index + 2;
			index = sql.indexOf("%", index);
			count++;
			end = sql.lastIndexOf("%");
		}
		if (count > 0) {
			sql = sql + " escape '\\' ";
		}
		return sql;
	}

	private static String insteadSlash(String sql) {

		int index = sql.indexOf("\\");

		while (index > 0) {
			sql = sql.substring(0, index) + "\\" + sql.substring(index);
			index = index + 2;
			index = sql.indexOf("\\", index);
		}
		return sql;
	}

	public static String getYear(java.util.Date date) {

		if (date == null) {
			return "";
		}

		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy");
		String temp = null;
		temp = simpleDate.format(date);

		if (temp == null)
			return "";
		return temp;
	}

	public static String getYMD(java.sql.Date date) {

		if (date == null) {
			return "";
		}

		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM/dd");
		String temp = null;
		temp = simpleDate.format(date);

		if (temp == null)
			return "";
		return temp;
	}

	public static String getYMDHM(java.sql.Date date) {

		if (date == null) {
			return "";
		}

		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy/MM/dd hh:mm");
		String temp = null;
		temp = simpleDate.format(date);

		if (temp == null)
			return "";
		return temp;
	}

	public static String getHM(java.sql.Date date) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat simpleDate = new SimpleDateFormat("hh:mm");
		String temp = null;
		temp = simpleDate.format(date);
		// MessageLog.info(null, temp);
		System.out.println(temp);
		if (temp == null)
			return "";
		return temp;

	}

	/**
	 * handle invalid input sql date: 2001.11.26 17:37
	 * 
	 * @return String
	 * @param sInput
	 *            String
	 */
	public static String handleWhereSql(String sInput) {

		if (sInput == null || sInput.trim().length() == 0)
			return sInput;

		StringBuffer sbTemp = new StringBuffer(sInput.length() + 4);

		char cAt;
		for (int i = 0; i < sInput.length(); i++) {
			cAt = sInput.charAt(i);
			if (cAt == '\'') {
				sbTemp.append("＇");
			} else if (cAt == '<') {
				sbTemp.append("＜");
			} else if (cAt == '>') {
				sbTemp.append("＞");
			} else if (cAt == '\"') {
				sbTemp.append("＂");
			} else if (cAt == '&') {
				sbTemp.append("＆");
			} else if (cAt == '\\') {
				sbTemp.append("＼");
			} else {
				sbTemp.append(cAt);
			}
		}
		return sbTemp.toString();
	}

	public static String clearUpString(String str) {

		StringBuffer strBuf = new StringBuffer(str);

		for (int i = 0; i < strBuf.length(); i++) {
			// replace the \'
			if (strBuf.charAt(i) == '\"') {
				strBuf.insert(i, '\"');
				i++;
			}// if
		}// for
		return strBuf.toString();
	}

	/**
	 * date:(2001-12-2 14:40:36)
	 * 
	 * @return java.lang.String
	 * @param str
	 *            java.lang.String
	 * @param length
	 *            int
	 */
	public static boolean checkLength(String str, int length) {

		if (str.length() > length) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * change the return to the string "\n" Creation date: (2001/12/22 10:04:54)
	 * 
	 * @return java.lang.String
	 * @param str
	 *            java.lang.String
	 */
	public static String changeReturn(String str) {
		str = CommonUtils.convertString(str);
		StringBuffer filtered = new StringBuffer(str.length());
		char c;
		for (int i = 0; i < str.length(); i++) {
			c = str.charAt(i);
			if (c == '\n') {
				filtered.append("\\n");
			} else if (c == '\r') {
				continue;
			} else {
				filtered.append(c);
			}
		}// for
		return filtered.toString();
	}

	/**
	 * Insert the method's description here. Creation date: (2001/12/23
	 * 17:11:46)
	 * 
	 * @return 1990Year1Month1Day
	 * @param str
	 *            like 1990/1/1
	 */
	/**
	 * public static String getDisplayStr4Date(String inStr) { if(inStr == null)
	 * { return ""; } String str = inStr; //at least 6 charactars in the str
	 * 1999/1 if(str.length() < 6) { return ""; }
	 * 
	 * str += "/"; StringBuffer rtnStr = new StringBuffer(str); int charPos = 0;
	 * int curPos = 0; curPos = str.indexOf('/'); if(curPos != -1){
	 * rtnStr.setCharAt(curPos,'年'); } charPos = curPos; curPos =
	 * str.indexOf('/',charPos + 1); if(curPos != -1){
	 * rtnStr.setCharAt(curPos,'月'); } charPos = curPos; curPos =
	 * str.indexOf('/',charPos + 1); if(curPos != -1){
	 * rtnStr.setCharAt(curPos,'日'); } return rtnStr.toString(); }
	 */

	/**
	 * Insert the method's description here. Creation date: (2001/12/23
	 * 17:52:20)
	 * 
	 * @return java.lang.String
	 * @param inStr
	 *            java.lang.String
	 */
	public static String getYYYYMMStr4Date(String inStr) {
		String str = inStr;
		String rtnStr = null;

		if (str.length() == 6) {
			String year = str.substring(0, 4);
			String month = str.substring(5, 6);
			rtnStr = year + "0" + month;
		}
		if (str.length() == 7) {
			String year = str.substring(0, 4);
			String month = str.substring(5, 7);
			rtnStr = year + month;
		}
		return rtnStr;
	}

	public static void closeJdbcArgs(ResultSet rs, PreparedStatement stmt,
			Connection conn) {

		if (rs != null) {

			// Always close the resultset
			try {
				rs.close();
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close resultset!");
				System.out.println("Can't close resultset!" + ex.toString());
			}
		}
		if (stmt != null) {

			// Always close the statement
			try {
				stmt.close();
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close preparedStatement!");
				System.out.println("Can't close preparedStatement!"
						+ ex.toString());
			}
		}
		if (conn != null) {

			// Always close the connection
			try {
				conn.close();
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close connection!");
				System.out.println("Can't close connection!" + ex.toString());
			}
		}

	}

	public static void closeJdbcArgs(ResultSet rs, Statement stmt,
			Connection conn) {

		if (rs != null) {

			// Always close the resultset
			try {
				rs.close();
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close resultset!");
				System.out.println("Can't close resultset!" + ex.toString());
			}
		}
		if (stmt != null) {

			// Always close the statement
			try {
				stmt.close();
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close statement!");
				System.out.println("Can't close statement!" + ex.toString());
			}
		}
		if (conn != null) {

			// Always close the connection
			try {
				conn.close();
				// MessageLog.info(null, "close connection successfully!");
				// System.out.println("close connection successfully!");
			} catch (SQLException ex) {
				// Ignore errors on close
				// MessageLog.warn(null, "Can't close connection!");
				System.out.println("Can't close connection!" + ex.toString());
			}
		}
	}

	/**
	 * String 对象转Date对象
	 * 
	 * @param date
	 * @param formatPattern
	 * @return
	 */
	public static Date string2Date(String date, String formatPattern) {
		SimpleDateFormat sdf = new SimpleDateFormat();
		try {
			if (date == null || "".equals(date)) {
				return null;
			}
			if ((formatPattern == null) || formatPattern.equals("")) {
				formatPattern = "yyyy-MM-dd";
			}
			sdf.applyPattern(formatPattern);
			return sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Date对象格式化为String对象
	 * 
	 * @param date
	 * @param formatPattern
	 * @return
	 */
	public static String date2String(Date date, String formatPattern) {
		String str = "";
		SimpleDateFormat sdf = null;
		try {
			if (formatPattern == null || formatPattern.equals("")) {
				formatPattern = "yyyy-MM-dd";
			}
			sdf = new SimpleDateFormat(formatPattern);
			str = sdf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return str;
	}

	public static String getTimeInMillis() {
		Calendar calendar = new GregorianCalendar();
		return String.valueOf(calendar.getTimeInMillis());
	}

	// public static String getByAttribute(HttpServletRequest request, String
	// str) {
	// str = request.getParameter(str);
	// if (str == null || str.equals("") || str.equals("null")) {
	// str = (String) request.getAttribute(str);
	// }
	// return dealNull(str);
	// }

	public static Connection openConn() {
		InitialContext context = null;
		DataSource dataSource = null;
		Connection conn = null;
		try {
			context = new InitialContext();
			dataSource = (DataSource) context.lookup("java:comp/env/sig");
			conn = dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (context != null) {
				// Always close the context
				try {
					context.close();
				} catch (NamingException ex) {
					// Ignore errors on close
					ex.printStackTrace();
				}
			}

		}
		return conn;
	}

	/**
	 * 数组转化为list
	 * 
	 * @param array
	 * @return
	 */
	public static List array2List(String[] array) {
		List list = new ArrayList();
		for (int i = 0; array != null && i < array.length; i++) {
			list.add(array[i]);
		}
		return list;
	}

	// /**
	// * 文件删除操作
	// * @param filePath
	// */
	// public static void fileDelete(HttpServletRequest request,String
	// filePath){
	//
	// if(filePath!=null && filePath!=""){
	// File mydelfile = new File(request.getRealPath("/")+ filePath);
	// if(mydelfile.exists()){
	// System.out.println("********文件存在***********");
	// mydelfile.delete();
	// System.out.println("********文件删除成功********");
	// }
	// }
	// }

	// /**
	// * 判断文件是否存在
	// * @param request
	// * @param filePath
	// * @return String
	// */
	// public static String fileExist(HttpServletRequest request,String
	// filePath){
	// if( filePath != null){
	// if(filePath != ""){
	// File file = new File(request.getRealPath("/")+ filePath);
	// if(!file.exists())
	// filePath = "";
	// }
	// } else filePath = "";
	// return filePath;
	// }

	public static String toDelNull(String str) {
		String result = toUnicode(str);
		if (result == null)
			result = "";
		return result;
	}

	/**
	 * 返回两个日期的年差
	 * 
	 * @param beforDate
	 *            　在前的日期
	 * @param afterDate
	 *            　在后的日期
	 * */
	public static int twoDateYear(Date beforDate, Date afterDate) {
		int year = 0;
		Calendar cal = Calendar.getInstance();
		cal.setTime(beforDate);
		int beforYear = cal.get(Calendar.YEAR);
		cal.setTime(afterDate);
		int afterYear = cal.get(Calendar.YEAR);
		if (beforYear < afterYear) {
			year = afterYear - beforYear;
		} else {
			year = beforYear - afterYear;
		}
		return year;
	}

	// /**
	// * 将Map中的数据以key作为属性ID, value作为属性值，设置到HttpServletRequest中
	// */
	// public static void setDataMap(Map dataMap, HttpServletRequest request) {
	// if (dataMap == null)
	// return;
	// Set set = dataMap.entrySet();
	// Iterator iter = set.iterator();
	// while (iter.hasNext()) {
	// Map.Entry mapentry = (Map.Entry) iter.next();
	// request.setAttribute(
	// mapentry.getKey().toString(),
	// mapentry.getValue());
	// }
	// }

	/**
	 * 判断对象是否为空
	 * 
	 * @param obj
	 * @return
	 */
	public static final boolean IsNull(Object obj) {
		if (obj == null) {
			return true;
		}
		if (obj instanceof String) {
			String strValue = (String) obj;
			if (strValue.trim().length() == 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 将utf-8编码转换成为gbk编码
	 * 
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static final String utf8Togbk(String str)
			throws UnsupportedEncodingException {
		if (str == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		/* 补全编码位置 */
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			switch (c) {
			case '+':
				sb.append(' ');
				break;
			case '%':
				try {
					sb.append((char) Integer.parseInt(
							str.substring(i + 1, i + 3), 16));
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException();
				}
				i += 2;
				break;
			default:
				sb.append(c);
				break;
			}
		}
		// 实施转换
		String result = sb.toString();
		String res = null;
		try {
			byte[] inputBytes = result.getBytes("8859_1");
			res = new String(inputBytes, "UTF-8");
		} catch (Exception e) {
		}
		return res;
	}

	/**
	 * 将GBK转换为UTF-8
	 * 
	 * @param mystr
	 * @return
	 */
	public static final String gbkToutf8(String mystr) {
		if (mystr == null) {
			return "";
		}
		int length = mystr.length();
		String[] s = new String[length * 3];
		byte[] b = new byte[length * 3];
		for (int i = 0; i < length; i++) {
			char c = mystr.charAt(i);
			String str = Integer.toBinaryString(c);
			int l = 16 - str.length();
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < l; j++) {
				sb.append("0");
			}
			sb.append(str);
			sb.insert(0, "1110");
			sb.insert(8, "10");
			sb.insert(16, "10");
			String str2 = sb.toString();
			s[i * 3] = str2.substring(0, 8);
			s[i * 3 + 1] = str2.substring(8, 16);
			s[i * 3 + 2] = str2.substring(16);
		}
		for (int i = 0; i < s.length; i++) {
			b[i] = Integer.valueOf(s[i], 2).byteValue();
		}
		String utf8 = null;
		try {
			utf8 = new String(b, "utf-8");
			System.out.println(utf8);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return utf8;
	}

	/**
	 * 字符串更换
	 * 
	 * @param value
	 * @param oleConetent
	 *            需要更换的
	 * @param newContent
	 *            替换为
	 * @return
	 */
	public static final String replaceValue(String value, String oleConetent,
			String newContent) {
		String returnValue = "";
		for (int i = 0; i < value.length(); i++) {
			/*
			 * String type=null; //将字串变量赋值为空,变量type必须要赋初值。
			 * if(localHost.length()<=0){//如果截取的长度小于等于0；
			 * type="输入的整形变量无效";//不合法，返回错误信息。 }else
			 * if(localHost.length()>value.length()){//如果截取的长度大于原始字串的长度。
			 * type="输入的整形变量超出范围";//不合法，返回错误信息。 }else{
			 * type=value.substring(value
			 * .indexOf(localHost),value.indexOf(localHost
			 * )+localHost.length());//将从字串的起点位置，到要终点位 System.out.println(type);
			 * }
			 */
			returnValue = value.replaceAll(oleConetent, newContent);
			if (value.indexOf(oleConetent) != -1) {
				break;
			}
		}
		return returnValue;
	}

	/**
	 * 取出所有的html元素
	 * 
	 * @param input
	 * @param length
	 * @return
	 */
	public static String dropTagHtml(String input, int length) {
		if (input == null || input.trim().equals("")) {
			return "";
		}
		// 去掉所有html元素,
		String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		str = str.replaceAll("[(/>)<]", "");
		int len = str.length();
		if (len <= length) {
			return str;
		} else {
			str = str.substring(0, length);
			str += "......";
		}
		return str;
	}
	
	/**
	 * 返回当前日期时间字符串<br>
	 * 默认格式:yyyy-mm-dd hh:mm:ss
	 * 
	 * @return String 返回当前字符串型日期时间
	 */
	public static String getCurrentTime() {
		String returnStr = null;
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		returnStr = f.format(date);
		return returnStr;
	}
	
	/**
	 * 生成制定长度的随机数字
	 * @param length	要生成的随机数次的长度
	 * @return
	 */
	public static String getRandomNum(int length){
		char mapTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		String randomNum = "";
		for (int i = 0; i < length; i++) {
			randomNum += mapTable[(int) (mapTable.length * Math.random())];
		}
		
		return randomNum;
	}
	
	/**
	 * 获取图片的Base64编码
	 * @param imgFilePath	图片路径
	 * @return
	 */
	public static String getImgBase64CodeStr(String imgFilePath) {
		byte[] data = null;
		try { 
			InputStream in = new FileInputStream(imgFilePath); 
			data = new byte[in.available()]; 
			in.read(data); 
			in.close(); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
		// 对字节数组Base64编码 
		BASE64Encoder encoder = new BASE64Encoder(); 
		return encoder.encode(data);// 返回Base64编码后的字节字符串
	}
	
	public static void main(String arts[]) {
		System.out.println(getImgBase64CodeStr("D:/developEnv/tomcat-6.0.18/webapps/wskh/uploadFiles/1329787159937.jpg"));
	}
}
